package com.zeta.edw.venus

import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SparkSession}

class ExtractKgmMeasure extends ExtractIncrement {

  override def readData(config: Config, schema: StructType)(implicit spark: SparkSession): DataFrame = {
    val query =
      s"""
         (
         SELECT
         PLATE_ID, OP_NO, ME_DATE, EQP_TYPE_ID, CELL_NO, MEAS_NO, PLOT_NO, MEAS_TYPE,
         TEST_EQP_NO, MEAS_VOL, MEAS_CUR, VS_VG, ION, IOFF, VTH,
         TO_CHAR(RESIST) AS RESIST,
         EXT_1, EXT_2, EXT_3, EXT_4, UPDATE_USER, UPDATE_DATE, MS_DATE, LOT_ID,
         PROD_ID, D_MS_DATE, D_ME_DATE, REWORK_NEW
         FROM ${config.jdbcTable}
         WHERE ${predicates(config, schema)}
         )
       """.stripMargin

    spark.read.format("jdbc")
      .option("url", config.jdbcUrl)
      .option("user", config.jdbcUsername)
      .option("password", config.jdbcPassword)
      .option("driver", "oracle.jdbc.OracleDriver")
      .option("fetchsize", "10000")
      .option("dbtable", query)
      .option("oracle.jdbc.ReadTimeout", s"${2 * 60 * 1000}")
      .option("oracle.net.CONNECT_TIMEOUT", s"${10 * 1000}")
      .load()
  }

}

object ExtractKgmMeasure extends ExtractKgmMeasure {
  def main(args: Array[String]): Unit = {
    startApp(args, new ExtractDataConfig())
  }
}